package com.powernode.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.BusinessEnum;
import com.powernode.domain.IndexImg;
import com.powernode.domain.Prod;
import com.powernode.feign.StoreFeignProd;
import com.powernode.mapper.IndexImgMapper;
import com.powernode.model.Result;
import com.powernode.service.IndexImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class IndexImgServiceImpl extends ServiceImpl<IndexImgMapper, IndexImg> implements IndexImgService{

    @Autowired
    private StoreFeignProd storeFeignProd;

    /**
     * 根据id查询轮播图,并且查询图片
     *
     * @param id
     * @return
     */
    @Override
    public IndexImg queryIndexImgById(Long id) {

        IndexImg byId = this.getById(id);

        if (byId == null){
            return null;
        }

        if (byId.getType() != 0){
            return byId;
        }

        // 查询关联的产品
        Result<List<Prod>> prodListByProdIds = storeFeignProd.getProdListByProdIds(Arrays.asList(byId.getProdId()));

        if (prodListByProdIds.getCode() == BusinessEnum.OPERATION_FAIL.getCode()){
            return byId;
        }

        List<Prod> data = prodListByProdIds.getData();

        Prod prod = data.get(0);
        byId.setPic(prod.getImgs());
        byId.setProdName(prod.getProdName());


        return byId;
    }
}
